﻿using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Interactivity;
using Hardcodet.Wpf.TaskbarNotification;

namespace AdTech.Snap
{
    /// <summary>
    /// A behavior for <see cref="Hardcodet.Wpf.TaskbarNotification.TaskbarIcon"/>.
    /// </summary>
    public class TaskbarIconBehavior : Behavior<TaskbarIcon>
    {
        // Fields
        private IBalloonTipHost host;

        // Dependency Properties
        /// <summary>
        /// For Internal Use only. Identifies the AdTech.Snap.TaskbarIconBehavior.DataContext dependency property.
        /// </summary>
        protected static readonly DependencyProperty DataContextProperty =
            DependencyProperty.Register("DataContext", typeof(object), typeof(TaskbarIconBehavior),
                new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnDataContextPropertyChanged)));

        // Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="AdTech.Snap.TaskbarIconBehavior"/> class.
        /// </summary>
        public TaskbarIconBehavior()
        {
            BindingOperations.SetBinding(this, DataContextProperty, new Binding());
        }

        // Methods
        /// <summary>
        /// This method is invoked whenever the behavior gets attached to the target <see cref="Hardcodet.Wpf.TaskbarNotification.TaskbarIcon"/>.
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            this.host = this.AssociatedObject.DataContext as IBalloonTipHost;
            if (this.host == null)
                return;

            this.host.ShowBalloonTip = (title, message, symbol) => this.AssociatedObject.ShowBalloonTip(title, message, symbol);
        }

        /// <summary>
        /// This method is invoked whenever the behavior gets detached from the target <see cref="Hardcodet.Wpf.TaskbarNotification.TaskbarIcon"/>.
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            if (host == null)
                return;

            this.host.ShowBalloonTip = null;
        }

        private static void OnDataContextPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TaskbarIconBehavior source = sender as TaskbarIconBehavior;
            if (source != null)
                source.OnAttached();
        }
    }

    /// <summary>
    /// Defines an interface for a control that hosts a <see cref="Hardcodet.Wpf.TaskbarNotification.TaskbarIcon"/> being able to show balloon tips.
    /// </summary>
    public interface IBalloonTipHost
    {
        /// <summary>
        /// Gets or sets the method to invoke in order to show a balloon tip on the target <see cref="Hardcodet.Wpf.TaskbarNotification.TaskbarIcon"/>.
        /// </summary>
        Action<string, string, BalloonIcon> ShowBalloonTip { get; set; }
    }
}
